package com.xju.config;

import com.wjx.mybatis.util.MyBatisSessionUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.io.InputStream;

@Configuration
public class AppConfig {
    @Bean
    public SqlSessionFactory sqlSessionFactory() {
        SqlSessionFactory factory = null;
        try {
            InputStream stream = Resources.getResourceAsStream("mybatis_config.xml");
            factory = new SqlSessionFactoryBuilder().build(stream);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("创建SqlSessionFactory错误");
        }
        return factory;
    }

    @Bean
    public MyBatisSessionUtil myBatisSessionUtil() {
        return MyBatisSessionUtil.me();
    }
}
